package cn.lg.soar.boot.client.utils;

import java.util.Calendar;

import cn.lg.soar.boot.client.entity.ClientUser;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.util.DigestUtils;

public class JWTUtil {

	private static String SECRET = "client";


	/**
	 * 生成 token
	 * @return token 加密的token
	 */
	public static String sign(ClientUser user) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE,10);
		String token = JWT.create()
//						.withClaim("userId", user.getId())
						.withClaim("key", DigestUtils.md5DigestAsHex(user.getSalt().getBytes()))
						.withExpiresAt(calendar.getTime())
						.sign(Algorithm.HMAC256(SECRET));
		return token;
	}

	/**
	 * 获取token信息 如果token有误则返回null
	 * @param token
	 * @return
	 */
	public static boolean getTokenInfo(String token){
		try {
			JWT.require(Algorithm.HMAC256(SECRET)).build().verify(token);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
}